package com.lh.tams.service.system;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.lh.tams.controller.qo.PersonQo;
import com.lh.tams.dao.system.PersonDao;
import com.lh.tams.domain.TamsPerson;
import com.lh.tams.exception.BusinessException;
import com.lh.tams.util.Constants;
import com.lh.tams.util.DataGridModel;
import com.lh.tams.util.Page;
import com.lh.tams.util.Status;

@Service
public class PersonService {
	private PersonDao personDao;

	public PersonDao getPersonDao() {
		return personDao;
	}

	public void setPersonDao(PersonDao personDao) {
		this.personDao = personDao;
	}

	public Page query(DataGridModel dgm,PersonQo personQo){
		//Page p=personDao.query(dgm, personQo);
		//personDao.update(p.getRows().get(0));
		return personDao.query(dgm, personQo);
	}
	
	public TamsPerson query(String userName){
		return personDao.query(userName);
	}
	
	/**
	 * 修改个人信息
	 * @param personUsername
	 * @param personName
	 * @param sex
	 * @param phone
	 * @param address
	 * @param remark
	 */
	public void modify(String personUsername,String personName,String sex,String phone,String address,String remark){
		TamsPerson person = personDao.query(personUsername);
		person.setPersonName(personName);
		person.setPersonSex(sex);
		person.setPhone(phone);
		person.setAddress(address);
		person.setRemark(remark);
		//更新person
		personDao.update(person);
	}
	/**
	 * 修改密码
	 * @param personUsername
	 * @param oldPwd
	 * @param newPwd1
	 * @param newPwd2
	 */
	public void modify(String personUsername,String oldPwd,String newPwd1,String newPwd2){
		TamsPerson person = personDao.query(personUsername,oldPwd);
		if(person==null){
			throw new BusinessException("旧密码不正确");
		}
		if(!newPwd1.equals(newPwd2)){
			throw new BusinessException("新密码不一致");
		}
		person.setPersonPassword(newPwd1);
		//更新person
		personDao.update(person);
	}
	
	public TamsPerson login(String personUsername,String personPassword){
		TamsPerson person=personDao.query(personUsername, personPassword);
		if(person==null)
			throw new BusinessException("用户名或密码不存在");
		if(person.getStatus()!=Status.STATUS_NORMAL)
			throw new BusinessException("用户状态异常，请联系管理员");
		return person;
	}
	
	public void add(TamsPerson tamsPerson){
		//查询是否存在重复警员编号的警员
		if(this.query(tamsPerson.getPersonUsername(),tamsPerson.getIdBgt())){
			throw new BusinessException("该警员编号已存在");
		}
		
		personDao.add(tamsPerson);
	}
	
	//根据警员编号是否存在警员
	public boolean query(String username,long id){
		return personDao.query(username,id);
	}
	
	public void update(TamsPerson tamsPerson){
		//查询是否存在重复警员编号的警员
		if(this.query(tamsPerson.getPersonUsername(),tamsPerson.getIdBgt())){
			throw new BusinessException("该警员编号已存在");
		}
		
		personDao.update(tamsPerson);
	}
	
	public void delete(long[] ids){
		personDao.delete(ids);
	}
	/**
	 * 批量重置密码
	 * @param ids
	 */
	public void resetPwd(long[] ids){
		
		List<TamsPerson> list = new ArrayList<TamsPerson>();
		//获取所有需要操作的警员  并重置密码
		for (int i = 0; i < ids.length; i++) {
			TamsPerson p = personDao.getPersonById(ids[i]);
			p.setPersonPassword(Constants.PASSWORD);
			list.add(p);
		}
		
		//循环更新
		for(TamsPerson p:list){
			personDao.update(p);
		}
		
	}
}
